<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
                <label class="el-form-item-label">ID</label>
                <el-input v-model="query.id" clearable placeholder="ID" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <label class="el-form-item-label">编码规则ID</label>
                <el-input v-model="query.codingId" clearable placeholder="编码规则ID" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                  <el-select v-model="query.ruleType" clearable filterable
                             placeholder="请选择" style="width: 185px;" class="filter-item" @change="crud.toQuery" >
                    <el-option
                            v-for="item in dict.rule_type"
                            :key="item.id"
                            :label="item.label"
                            :value="item.value" />
                  </el-select>
                <label class="el-form-item-label">规则顺序</label>
                <el-input v-model="query.ruleSort" clearable placeholder="规则顺序" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <label class="el-form-item-label">规则内容</label>
                <el-input v-model="query.ruleContent" clearable placeholder="规则内容" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <label class="el-form-item-label">生成的内容</label>
                <el-input v-model="query.finalContent" clearable placeholder="生成的内容" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <label class="el-form-item-label">规则JSON</label>
                <el-input v-model="query.ruleJson" clearable placeholder="规则JSON" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <label class="el-form-item-label">描述</label>
                <el-input v-model="query.describes" clearable placeholder="描述" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="规则类型" prop="ruleType">
            <el-select v-model="form.ruleType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.rule_type"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="规则顺序" prop="ruleSort">
            <el-input v-model="form.ruleSort" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="规则内容" prop="ruleContent">
            <el-input v-model="form.ruleContent" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="生成的内容" prop="finalContent">
            <el-input v-model="form.finalContent" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="规则JSON" prop="ruleJson">
            <el-input v-model="form.ruleJson" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.describes" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="ruleType" label="规则类型">
          <template slot-scope="scope">
            {{ dict.label.rule_type[scope.row.ruleType] }}
          </template>
        </el-table-column>
        <el-table-column prop="ruleSort" label="规则顺序" />
        <el-table-column prop="ruleContent" label="规则内容" />
        <el-table-column prop="finalContent" label="生成的内容" />
        <el-table-column prop="ruleJson" label="规则JSON" />
        <el-table-column prop="describes" label="描述" />
        <el-table-column v-if="checkPer(['admin','productCodingRule:edit','productCodingRule:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProductCodingRule from '@/api/product/productCodingRule'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, codingId: null, ruleType: null, ruleSort: null, ruleContent: null, finalContent: null, ruleJson: null, describes: null }
export default {
  name: 'ProductCodingRule',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['rule_type'],
  cruds() {
    return CRUD({ title: '产品编码规则', url: 'api/productCodingRule', idField: 'id', sort: 'id,desc', crudMethod: { ...crudProductCodingRule }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'productCodingRule:add'],
        edit: ['admin', 'productCodingRule:edit'],
        del: ['admin', 'productCodingRule:del']
      },
      rules: {
        ruleType: [
          { required: true, message: '规则类型不能为空', trigger: 'blur' }
        ],
        ruleSort: [
          { required: true, message: '规则顺序不能为空', trigger: 'blur' }
        ],
        ruleContent: [
          { required: true, message: '规则内容不能为空', trigger: 'blur' }
        ],
        finalContent: [
          { required: true, message: '生成的内容不能为空', trigger: 'blur' }
        ],
        ruleJson: [
          { required: true, message: '规则JSON不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'codingId', display_name: '编码规则ID' },
        { key: 'ruleType', display_name: '规则类型' },
        { key: 'ruleSort', display_name: '规则顺序' },
        { key: 'ruleContent', display_name: '规则内容' },
        { key: 'finalContent', display_name: '生成的内容' },
        { key: 'ruleJson', display_name: '规则JSON' },
        { key: 'describes', display_name: '描述' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
